package com.htj.admin.controller;

import com.htj.admin.domain.AdminUser;
import com.htj.admin.service.AdminUserServiceInf;
import com.htj.common.Result;
import com.htj.util.JWTUtil;
import com.htj.vo.PageListVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "admin")
@CrossOrigin
public class AdminUserController {
    @Autowired
    AdminUserServiceInf adminUserServiceInf;

    /**
     * 后台登录
     * @param map
     * admin 后台账号
     * password 后台密码
     * @return
     * code码
     * token
     */
    @PostMapping("/login")
    public Result<Map> login(@RequestBody Map<String,String> map){
        AdminUser adminUser = adminUserServiceInf.login(map.get("loginAct"),map.get("loginPwd"));
        Map<String,Object> res = new HashMap<>();
        if(adminUser != null){
            Map<String,String> m = new HashMap<>();
            //在payload中存id和name
            m.put("id", String.valueOf(adminUser.getId()));
            m.put("name", adminUser.getName());
            String token = JWTUtil.getToken(m);
            //返回给前端的
            res.put("user",adminUser);
            res.put("token",token);
            Result<Map> result = Result.succ(res);
            return result;
        }
        res.put("msg","账号或密码错误");
        return Result.fail(res);
    }

    /**
     * 绑定邮箱
     * @param email
     * @return
     * 是否成功
     */
    @PostMapping("/bindEmail")
    public Result<String> bindEmail(@RequestBody Map<String,String> email, @RequestHeader String token){
        String id = JWTUtil.verify(token).getClaim("id").asString();
        boolean bool = adminUserServiceInf.bindEmail(id,email.get("email"));
        return bool?Result.succ():Result.fail();
    }

    /**
     * 新增管理员
     * @param adminUser
     * @return
     */
    @PostMapping("/save")
    public Result<String> add(@RequestBody AdminUser adminUser){
        boolean bool = adminUserServiceInf.add(adminUser);
        return bool?Result.succ():Result.fail();
    }

    /**
     * 修改管理员
     * @param adminUser
     * @return
     */
    @PutMapping("/save")
    public Result<String> update(@RequestBody AdminUser adminUser){
        boolean bool = adminUserServiceInf.update(adminUser);
        return bool?Result.succ():Result.fail();
    }
    /**
     * 分页页面分页
     * @param currentPage
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/pageList")
    public Result<PageListVo> pageList(@RequestParam String currentPage,
                                       @RequestParam String pageSize,
                                       @RequestParam String name){
        HashMap<String, String> map = new HashMap<>();
        map.put("currentPage",currentPage);
        map.put("pageSize",pageSize);
        map.put("name",name);
        PageListVo<AdminUser> pageList = adminUserServiceInf.pageList(map);
        return Result.succ(pageList);
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public Result<String> delete(@PathVariable Integer id){
        boolean bool = adminUserServiceInf.delete(id);
        return bool?Result.succ():Result.fail();
    }
    /**
     * 批量删除
     * @param list
     * @return
     */
    @PostMapping("/deleteByIds")
    public Result<String> deleteByIds(@RequestBody List<Integer> list){
        boolean bool = true;
        for(Integer id : list){
            bool = adminUserServiceInf.delete(id);
        }
        return bool?Result.succ():Result.fail();
    }
    /**
     * 改变管理员使用状态
     * @param map
     * @return
     */
    @PostMapping("/changeStatus")
    public Result<String> changeStatus(@RequestBody Map<String,Integer> map){
        boolean bool = adminUserServiceInf.changeStatus(map);
        return bool?Result.succ():Result.fail();
    }

    /**
     * 通过id找用户
     * @param id
     * @return
     */
    @GetMapping("/getUserById")
    public Result<AdminUser> getUserById(@RequestParam String id){
        boolean bool = false;
        AdminUser adminUser = adminUserServiceInf.getUserById(id);
        if(adminUser != null){
            return Result.succ(adminUser);
        }
        return Result.fail();
    }
}
